/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.message;

import mbean.admin.driver.*;
import mbean.admin.customer.*;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlInputText;
import javax.inject.Inject;
import mbean.AbstractMB;
import mbean.shiroMB;
import model.entity.Customer;
import model.entity.Depot;
import model.entity.Driver;
import model.entity.Message;
import model.entity.Role;
import model.entity.User;
import model.entity.Vehicle;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.model.LazyDataModel;
import service.IUserService;
import service.InterfaceServiceLocal;
import utils.QueryStr;

/**
 *
 * @author dingnangen
 */
@Named(value = "messageCreateMB")
@SessionScoped
public class MessageCreateMB extends AbstractMB implements Serializable {

    @Inject
    shiroMB shirMB;
    @EJB(beanName = "MessageManager")
    private InterfaceServiceLocal<Message> messageManager;
    @EJB(beanName = "DriverManager")
    private InterfaceServiceLocal<Driver> driverManager;
    @EJB(beanName = "VehicleManager")
    private InterfaceServiceLocal<Vehicle> vehicleManager;
    @EJB(beanName = "RoleManager")
    private InterfaceServiceLocal<Role> roleManager;
    @EJB(beanName = "UserManager")
    private IUserService<User> userManager;
    @EJB(beanName = "DepotManager")
    private InterfaceServiceLocal<Depot> depotManager;
    private Driver currentdriver;

    private List<User> userList;
    private List<Role> roleList;
    private String messageRange = "user";
    private Message msg;
    private String forid;
    List<KV> results= new ArrayList<KV>();;

    /**
     * 初始化
     */
    public void init() {
        msg = new Message();
//        msg.setIdmessage(1233);

    }

    public List<KV> completeText(String query) {
        List p = new ArrayList();
        QueryStr queryStr;
        results = new ArrayList<KV>();
        switch (messageRange) {

            case "user":
                queryStr = new QueryStr("User", 0);
                queryStr.addLikeCondition("where", "name");
                p.add("%" + query + "%");
                System.out.println(queryStr.getQueryList());
                List<User> userList = userManager.findAll(queryStr.getQueryList(), p);
                for (User u : userList) {

                    KV kv = new KV(u.getIduser() + "", u.getName());

                    results.add(kv);
                }
                break;
            case "role":
                queryStr = new QueryStr("Role", 0);
                queryStr.addLikeCondition("where", "roleName");
                p.add("%" + query + "%");
                List<Role> roleList = roleManager.findAll(queryStr.getQueryList(), p);
                for (Role r : roleList) {
                    KV kv = new KV(r.getRoleid() + "", r.getRoleName());
                    results.add(kv);

                }
                break;
        }

        return results;
    }

    public void precreateMsg() {
        if (Faces.isPostback()) {
            System.out.println("isPostback" + messageRange);
            completeText("");
            return;
        }
        init();
    }

    public void create() {
        System.out.println(messageRange);
        List p = new ArrayList();
        p.add(shirMB.getLoginid());
        try {
            switch (messageRange) {

                case "user":
                    User user = userManager.find(Integer.parseInt(forid));
                    msg.setCreatetime(new Date());
                    msg.setCreateuserid(userManager.findOneItem("select u from User u where u.loginname=?1", p));
                    msg.setRecuserid(user);
                    this.messageManager.create(msg);
                    break;
                case "role":
                    Role role = roleManager.find(Integer.parseInt(forid));
                    msg.setCreatetime(new Date());
                    msg.setCreateuserid(userManager.findOneItem("select u from User u where u.loginname=?1", p));
                    msg.setRecrole(role);
                    this.messageManager.create(msg);
                    break;
            }

            System.out.println("create finish");
        } catch (Exception e) {
            System.out.println("failed");
            Messages.create(Faces.getResourceBundle("bundle").getString("failed")).add();
        }
        forid=null;
        Messages.create(Faces.getResourceBundle("bundle").getString("saveSuccess")).add();

    }

    /**
     * Creates a new instance of CustomerCreateMB
     */
    public MessageCreateMB() {
    }

    public Driver getCurrentdriver() {
        return currentdriver;
    }

    public void setCurrentdriver(Driver currentdriver) {
        this.currentdriver = currentdriver;
    }

    public String getMessageRange() {
        return messageRange;
    }

    public void setMessageRange(String messageRange) {
        this.messageRange = messageRange;
    }

    public Message getMsg() {
        return msg;
    }

    public void setMsg(Message msg) {
        this.msg = msg;
    }

    public String getForid() {
        return forid;
    }

    public void setForid(String forid) {
        this.forid = forid;
    }

    public class KV {

        private String k;
        private String v;

        public KV(String k, String v) {
            this.k = k;
            this.v = v;
        }

        public String getK() {
            return k;
        }

        public void setK(String k) {
            this.k = k;
        }

        public String getV() {
            return v;
        }

        public void setV(String v) {
            this.v = v;
        }

    }

}
